package come.bjpowernode.javase.reentrantlock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class DeadLock {
    static Lock lock1 = new ReentrantLock();
    Lock lock2 = new ReentrantLock();
    public static void main(String[] args) {
        DeadLock deadLock = new DeadLock();
      Thread thread = new Thread(new ThreadDemo(lock1,deadLock.lock2)) ;
      Thread thread1 = new Thread(new ThreadDemo(deadLock.lock2, deadLock.lock1)) ;
    thread.start();
    thread1.start();
    thread.interrupt();
    }


}

class ThreadDemo implements Runnable{
    Lock firstLock;
    Lock secondLock;

    public ThreadDemo(Lock firstLock,Lock secondLock){
        this.firstLock = firstLock;
        this.secondLock = secondLock;
    }

    @Override
    public void run() {
        try{
            firstLock.lockInterruptibly();
            TimeUnit.MICROSECONDS.sleep(50);
            secondLock.lockInterruptibly();
        }catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            firstLock.unlock();
            secondLock.unlock();
            System.out.println(Thread.currentThread().getName()+"获取到了资源，正常结束了！");
        }
    }
}